package cn.sxt.questions.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.questions.dao.AnswerDao;
import cn.sxt.questions.dao.ColumnsDao;
import cn.sxt.questions.dao.ItemsDao;
import cn.sxt.questions.dao.QuestionDao;
import cn.sxt.questions.pojo.Answer;
import cn.sxt.questions.pojo.Columns;
import cn.sxt.questions.pojo.Items;
import cn.sxt.questions.pojo.Question;
import cn.sxt.questions.service.IQuestionService;

@Service("questionService")
public class IQuestionServiceImpl implements IQuestionService{
	@Resource
	private ColumnsDao columnsDao;
	@Resource
	private ItemsDao itemsDao;
	@Resource
	private QuestionDao questionDao;
	@Resource
	private AnswerDao answerDao;
	
	@Override
	public Columns findOneColumnsService(Map<String, Object> condMap) {
		return this.columnsDao.findOne(condMap);	
	}
	
	@Override
	public JSONObject saveService(Columns columns) {
		JSONObject resultJson=new JSONObject();
		int res=this.columnsDao.save(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Columns columns) {
		JSONObject resultJson=new JSONObject();
		int res=this.columnsDao.update(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Columns columns) {
		JSONObject resultJson=new JSONObject();
		int res=this.columnsDao.delete(columns);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", columns.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Columns> findListColumnsService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Columns> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Columns> columnsList=this.columnsDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return columnsList;
		}
		return this.columnsDao.findList( condMap);
	}

	
	/**
	 * Items
	 */
	@Override
	public Items findOneItemsService(Map<String, Object> condMap) {
		return this.itemsDao.findOne(condMap);	
	}
	
	@Override
	public JSONObject saveService(Items items) {
		JSONObject resultJson=new JSONObject();
		int res=this.itemsDao.save(items);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", items.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Items items) {
		JSONObject resultJson=new JSONObject();
		int res=this.itemsDao.update(items);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", items.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Items items) {
		JSONObject resultJson=new JSONObject();
		int res=this.itemsDao.delete(items);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", items.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Items> findListItemsService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Items> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Items> itemsList=this.itemsDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return itemsList;
		}
		return this.itemsDao.findList(condMap);
	}

	/**
	 * question
	 */
	@Override
	public Question findOneQuestionService(Map<String, Object> condMap) {
		return this.questionDao.findOne(condMap);	
	}
	
	@Override
	public JSONObject saveService(Question question) {
		JSONObject resultJson=new JSONObject();
		int res=this.questionDao.save(question);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", question.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Question question) {
		JSONObject resultJson=new JSONObject();
		int res=this.questionDao.update(question);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", question.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Question question) {
		JSONObject resultJson=new JSONObject();
		int res=this.questionDao.delete(question);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", question.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Question> findListQuestionService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Question> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Question> questionList=this.questionDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return questionList;
		}
		return this.questionDao.findList( condMap);
	}


	@Override
	public Answer findOneAnswerService(Map<String, Object> condMap) {
		return this.answerDao.findOne(condMap);	
	}
	
	@Override
	public JSONObject saveService(Answer answer) {
		JSONObject resultJson=new JSONObject();
		int res=this.answerDao.save(answer);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", answer.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Answer answer) {
		JSONObject resultJson=new JSONObject();
		int res=this.answerDao.update(answer);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", answer.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Answer answer) {
		JSONObject resultJson=new JSONObject();
		int res=this.answerDao.delete(answer);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", answer.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Answer> findListAnswerService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Answer> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Answer> answerList=this.answerDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return answerList;
		}
		return this.answerDao.findList( condMap);
	}

	
}
